package pa3.gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import javax.swing.JButton;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneLayout;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import pa3.Host;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class HostViewNew extends javax.swing.JFrame {
	private JTextArea logArea;
	private JButton killButton;
	private JScrollPane logScrollPane;
	private JProgressBar jProgressBar1;
	private JLabel hostStatus;
	private JLabel hostInfo;
	private HashMap<String, JProgressBar> progressBarsMap = new HashMap<String, JProgressBar>();

	private String hostName;
	private int failureProbability;
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this JFrame
	*/
	/*
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				HostViewNew inst = new HostViewNew();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	*/
	public HostViewNew(String hostName, int failureProbability) {
		super();
		this.hostName = hostName;
		this.failureProbability = failureProbability;
		initGUI();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
			thisLayout.rowHeights = new int[] {7, 7, 7, 7, 7, 7};
			thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {7, 7, 7, 7};
			getContentPane().setLayout(thisLayout);
			{
				hostInfo = new JLabel();
				hostInfo.setText("Name: " + this.hostName + ", " + this.failureProbability + "% fail rate");
				getContentPane().add(hostInfo, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				hostStatus = new JLabel();
				getContentPane().add(hostStatus, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				hostStatus.setText("status");
				hostStatus.setFont(new java.awt.Font("Calibri",1,14));
			}
			{
				logScrollPane = new JScrollPane();
				getContentPane().add(logScrollPane, new GridBagConstraints(0, 1, 3, 4, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				logScrollPane.setPreferredSize(new java.awt.Dimension(499, 159));
				{
					logArea = new JTextArea();
					logScrollPane.setViewportView(logArea);
					logScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
					logArea.setEditable(false);
					//logArea.setPreferredSize(new java.awt.Dimension(259, 16));
					logArea.setFont(new java.awt.Font("Monospaced",0,11));
				}
			}
			{
				killButton = new JButton();
				getContentPane().add(killButton, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				killButton.setText("crash");
				killButton.setVisible(false);
			}
			pack();
			this.setSize(559, 257);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	public JButton getKillButton(){
		return killButton;
	}
	/*
	void addProgressBar(String name){
		progressBarsMap.put(name, pb);
		pack();
	}
	
	void removeProgressBar(String name){
		if(progressBarsMap.get(name) != null)
		{
			progressBars.remove(progressBarsMap.get(name));
			progressBarsMap.remove(name);
			pack();
		}
	}
	
	void updateProgressBar(String name, int value){
		if(progressBarsMap.get(name) != null){
			progressBarsMap.get(name).setValue(value);
		}
	}
	*/
	void setHostStatus(String status){
		hostStatus.setText(status);
	}

	public void setLogString(String log) {
		logArea.setText(log);
		logArea.getCaret().setDot( logArea.getText().length() );
		logScrollPane.scrollRectToVisible(logArea.getVisibleRect() );
		//logArea.setCaretPosition(logArea.getCaretPosition()+logArea.getText().length());
	}

}
